﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using WMSADL.SY;
using WMSAML.SY;
using System.Transactions;

namespace WMSABL.SY
{
    public class TSystemRegistBLL
    {
        PublicDataSet bll_comm = new PublicDataSet();
        TSystemMenuDAL dal_menu = new TSystemMenuDAL();
        TSystemRegistDAL dal_Regist = new TSystemRegistDAL();
        public bool ConnectionSQL()
        {
            return dal_Regist.ConnectionSQL();
        }
        public bool ConnectionSQL(string conn)
        {
            return dal_Regist.ConnectionSQL(conn);
        }
        public TSystemMenuEntity Add_SystemMenu(TSystemMenuEntity entity)
        {
            TSystemMenuEntity entity_SystemMenu = new TSystemMenuEntity();
            int resultAdd = dal_menu.Add_SystemMenu(entity);
            if (resultAdd > 0)
            {
                entity_SystemMenu = GetEntity_SystemMenu(resultAdd);
                if (entity_SystemMenu != null)
                {
                    int headid = entity_SystemMenu.HeadID;
                    string family = "";
                    if (bll_comm.GetDataSet("TSystemMenu", " HeadID=" + headid + " ").Tables[0].Rows.Count > 1)
                    {
                        family = (Convert.ToInt64(dal_menu.GetEntity_SystemMenuByHeadID(entity.HeadID).Family) + 1).ToString();
                    }
                    else
                    {
                        family = dal_menu.GetEntity_SystemMenu(entity.HeadID).Family + "01";
                    }
                    entity_SystemMenu.Family = family;
                    this.Update_SystemMenu(entity_SystemMenu);
                }
            }
            return entity_SystemMenu;
        }
        public bool Update_SystemMenu(TSystemMenuEntity entity)
        {
            return dal_menu.Update_SystemMenu(entity);
        }

        /// <summary>
        /// 排序升序
        /// </summary>
        public bool Update_SystemMenuFamilyUp(int id)
        {
            TSystemMenuEntity entity = new TSystemMenuEntity();
            entity = this.GetEntity_SystemMenu(id);
            long family = Convert.ToInt64(entity.Family) - 1;
            if (dal_menu.Update_SystemMenuFamily(id, family.ToString()))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 排序降序
        /// </summary>
        public bool Update_SystemMenuFamilyDown(int id)
        {
            TSystemMenuEntity entity = new TSystemMenuEntity();
            entity = this.GetEntity_SystemMenu(id);
            long family = Convert.ToInt64(entity.Family) + 1;
            return dal_menu.Update_SystemMenuFamily(id, family.ToString());
        }

        public TSystemMenuEntity GetEntity_SystemMenu(int id)
        {
            return dal_menu.GetEntity_SystemMenu(id);
        }

        public DataSet GetList_SystemMeun()
        {
            return dal_menu.GetList_SystemMeun("1");
        }

        /// <summary>
        /// 报表中心
        /// </summary>
        /// <returns>DataSet</returns>
        public DataSet GetList_SystemMeunReport()
        {
            return dal_menu.GetList_SystemMeunReport("1");
        }

        //**************************SystemRegist********************************//
        public bool Validate_SystemRegist(string pcCode)
        {
            return dal_Regist.Validate_SystemRegist(pcCode);
        }
        public bool Validate_SystemRegist()
        {
            return dal_Regist.Validate_SystemRegist();
        }
        public TSystemRegistEntity GetEntity_SystemRegist(string pcCode)
        {
            return dal_Regist.GetEntity_SystemRegist(pcCode);
        }
        public bool Add_SystemRegist(TSystemRegistEntity entity, string[] arr)
        {
            bool falg = false;
            TransactionOptions transactionOption = new TransactionOptions();
            //设置事务隔离级别
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            // 设置事务超时时间
            transactionOption.Timeout = new TimeSpan(0, 0, 10);
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, transactionOption))
            {
                try
                {
                    #region
                    if (dal_Regist.Add_SystemRegist(entity) > 0)
                    {
                        dal_menu.Update_SystemMenuDefault();
                        int result = 0;
                        for (int i = 0; i < arr.Length; i++)
                        {
                            dal_menu.Update_SystemMenuShow(Convert.ToInt32(arr[i]));
                            result++;
                        }
                        if (result == arr.Length)
                        {
                            falg = true;
                            ts.Complete();
                        }
                        else
                        {
                            ts.Dispose();
                        }
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    throw new Exception("信息异常,原因:" + ex.Message);
                }
                finally
                {
                    ts.Dispose();
                }
                return falg;
            }
        }

        public bool Delete_SystemRegist(string pcCode)
        {
            return dal_Regist.Del_SystemRegist(pcCode);
        }
    }
}
